package com.margo.project.limit.handle;


import org.springframework.stereotype.Component;

@Component
public class LeakeyBucketLimiter {
	private int capacity=10;
	private int water=0;
	private int rate=4;
	private long lastTime=System.currentTimeMillis();
	
	public synchronized int acquire() {
		long now=System.currentTimeMillis();
		int decrement=(int)(now-lastTime)*rate/1000;
		if(decrement>water) {
			water=0;
		}else {
			water=water-decrement;
			
		}
		if(water+1>capacity) {
			return 1;
		}else {
			lastTime=now;
			water++;
		}
		return 2;
	}
}
